package com.emin.platform.productCenter.multidatasource;

import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.hibernate.internal.SessionFactoryImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate5.HibernateTransactionManager;

import javax.sql.DataSource;
import java.util.Set;

/**
 * 此类用于创建事物
 *
 * @author Jack
 * @version V1.0
 * @Description: 此类用于创建此类用于创建事物
 * @date 2017/10/24 14:42
 */
public class MultiTransactionManager extends HibernateTransactionManager {
    private static final long serialVersionUID = -8255506659278029396L;
    private transient Logger logger = Logger.getLogger(MultiTransactionManager.class);
    private transient MultiSessionFactory multiSessionFactory;
    @Autowired
    @Qualifier("sessionFactory")
    private transient MultiSessionFactoryBean eminSessionFactory;

    public MultiTransactionManager() {
        System.out.println(1111111);
    }

    public void setMultiSessionFactory(MultiSessionFactory multiSessionFactory) {
        this.multiSessionFactory = multiSessionFactory;
    }

    public MultiSessionFactory getMultiSessionFactory() {
        return this.multiSessionFactory;
    }

    /**
     * 获取SessionFactory
     *
     * @return SessionFactory
     * @throws
     * @Description 首先从MultiSessionFactory中取，没有则使用eminSessionFactory创建
     * @author Jack
     * @date 2017/10/24
     */
    public SessionFactory getSessionFactory() {
        SessionFactory sessionFactory = MultiSessionFactory.getInstance().getSessionFactory();
        if (sessionFactory == null) {
            this.logger.info("Initializing session factory for the company");
            try {
                sessionFactory = this.eminSessionFactory.bulidSessionFactoryByDataSourceKey();
            } catch (Exception var4) {
                var4.printStackTrace();
            }
            if (sessionFactory == null) {
                this.logger.error("Initializing session factory for the company fail");
            }
        }
        return sessionFactory;
    }

    @Override
    public void afterPropertiesSet() {
        logger.info("afterPropertiesSet in MultiTransactionManager and checking the session " +
                "factory and data source");
        if (multiSessionFactory == null) {
            logger.error("afterPropertiesSet in MultiTransactionManager session error");
        }
        Set<Long> companyIds = multiSessionFactory.getSessionFactoryKeys();
        for (Long companyId : companyIds) {
            SessionFactory sessionFactory = multiSessionFactory.getSessionFactory(companyId);
            if (sessionFactory == null) {
                logger.error("afterPropertiesSet in MultiTransactionManager sessionFactory error");
            }
            DataSource ds = MultiDataSource.getInstance().getLocalDataSourceByConnectionProvider(
                    (SessionFactoryImpl) sessionFactory);
            if (ds == null) {
                logger.error("afterPropertiesSet in MultiTransactionManager ds error");
            }
        }
    }

    public DataSource getDataSource() {
        return MultiDataSource.getInstance().getDataSource();
    }

    public void setEminSessionFactory(MultiSessionFactoryBean eminSessionFctory) {
        this.eminSessionFactory = eminSessionFctory;
    }
}
